/**
 * @filename:${entityName}Controller ${createTime}
 * @project ${project}  ${version}
 * Copyright(c) 2018 ${author} Co. Ltd. 
 * All right reserved. 
 */
package ${controllerUrl};

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springmvc.common.JsonResult;
import ${entityUrl}.${entityName};
import ${daoUrl}.${entityName}Dao;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**   
 * 
 * @Description:  ${entityName}接口层
 * @Author:       ${author}   
 * @CreateDate:   ${createTime}
 * @Version:      ${version}
 *    
 */
@Api(description = "${entityName}",value="${entityName}" )
@RestController
@RequestMapping("/${objectName}")
public class ${entityName}Controller {

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	public ${entityName}Dao ${objectName}ServiceImpl;

<#if (keys?size==1)>
	/**
	 * @explain 查询${entityName}对象  <swagger GET请求>
	 * @param   对象参数：id
	 * @return  ${objectName}
	 * @author  ${author}
	 * @time    ${createTime}
	 */
	@GetMapping("/get${entityName}ById/{id}")
	@ApiOperation(value = "获取${entityName}信息", notes = "获取${entityName}信息[${objectName}]，作者：${author}")
	@ApiImplicitParam(paramType="path", name = "id", value = "${entityName}id", required = true, dataType = "${idType}")
	public String get${entityName}ById(@PathVariable("id")${idType} id){
		JsonResult result=new JsonResult();
		try {
			${entityName} ${objectName}=${objectName}ServiceImpl.selectByPrimaryKey(id);
			if (${objectName}!=null) {
				result.setCode(1);
				result.setMessage("成功");
				result.setData(${objectName});
			} else {
				logger.error("获取${entityName}失败ID："+id);
				result.setCode(-1);
				result.setMessage("你获取的${entityName}不存在");
			}
		} catch (Exception e) {
			logger.error("获取${entityName}执行异常："+e.getMessage());
			result.setCode(-1);
			result.setMessage("执行异常，请稍后重试");
		}

	Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().serializeNulls()
	.create();
	return gson.toJson(result);

	}

	/**
	* @explain 删除${entityName}对象
	* @param   对象参数：id
	* @return  int
	* @author  ${author}
	* @time    ${createTime}
	*/
	@PostMapping("/deleteByPrimaryKey")
	@ApiOperation(value = "删除${entityName}", notes = "删除${entityName},作者：${author}")
	@ApiImplicitParam(paramType="query", name = "id", value = "${entityName}id", required = true, dataType = "${idType}")
	public String deleteByPrimaryKey(${idType} id){
	JsonResult result=new JsonResult();
				try {
				int reg=${objectName}ServiceImpl.deleteByPrimaryKey(id);
				if (reg>0) {
				result.setCode(1);
				result.setMessage("成功");
				result.setData(id);
				} else {
				logger.error("删除${entityName}失败ID："+id);
				result.setCode(-1);
				result.setMessage("执行错误，请稍后重试");
				}
				} catch (Exception e) {
				logger.error("删除${entityName}执行异常："+e.getMessage());
				result.setCode(-1);
				result.setMessage("执行异常，请稍后重试");
				}
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().serializeNulls()
			.create();
			return gson.toJson(result);
				}
	<#else>
	</#if>
	/**
	 * @explain 添加${entityName}对象
	 * @param   对象参数：${objectName}
	 * @return  int
	 * @author  ${author}
	 * @time    ${createTime}
	 */
	@PostMapping("/insertSelective")
	@ApiOperation(value = "添加${entityName}", notes = "添加${entityName}[${objectName}],作者：${author}")
	public String insertSelective(@RequestBody ${entityName} ${objectName}){
	JsonResult result=new JsonResult();
		try {
			int rg=${objectName}ServiceImpl.insertSelective(${objectName});
			if (rg>0) {
				result.setCode(1);
				result.setMessage("成功");
				result.setData(${objectName});
			} else {
				logger.error("添加${entityName}执行失败："+${objectName}.toString());
				result.setCode(-1);
				result.setMessage("执行失败，请稍后重试");
			}
		} catch (Exception e) {
			logger.error("添加${entityName}执行异常："+e.getMessage());
			result.setCode(-1);
			result.setMessage("执行异常，请稍后重试");
		}
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().serializeNulls()
			.create();
			return gson.toJson(result);
	}


		@GetMapping("/query${entityName}All")
		@ApiOperation(value = "获取${entityName}", notes = "获取全部${entityName},作者：${author}")
		public String query${entityName}All(@RequestBody ${entityName} ${objectName}){
		JsonResult result=new JsonResult();
		try {
		List<${entityName}> list=${objectName}ServiceImpl.query${entityName}All();
		if (list!= null) {
		result.setCode(1);
		result.setMessage("成功");
		result.setData(list);
		} else {

		result.setCode(-1);
		result.setMessage("执行错误，请稍后重试");
		}
		} catch (Exception e) {
		logger.error("获取${entityName}执行异常："+e.getMessage());
		result.setCode(-1);
		result.setMessage("执行异常，请稍后重试");
		}
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().serializeNulls()
		.create();
		return gson.toJson(result);
		}


		@PostMapping("/deleteList")
		@ApiOperation(value = "删除${entityName}", notes = "删除${entityName},作者：${author}")
		public String deleteList(@RequestBody ${entityName} ${objectName}){
		JsonResult result=new JsonResult();
		try {
		int reg=${objectName}ServiceImpl.deleteList(${objectName});
		if (reg>0) {
		result.setCode(1);
		result.setMessage("成功");
		} else {

		result.setCode(-1);
		result.setMessage("执行错误，请稍后重试");
		}
		} catch (Exception e) {
		logger.error("删除${entityName}执行异常："+e.getMessage());
		result.setCode(-1);
		result.setMessage("执行异常，请稍后重试");
		}
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().serializeNulls()
		.create();
		return gson.toJson(result);
		}




		/**
	 * @explain 修改${entityName}对象
	 * @param   对象参数：${objectName}
	 * @return  ${objectName}
	 * @author  ${author}
	 * @time    ${createTime}
	 */
	@ApiOperation(value = "修改${entityName}", notes = "修改${entityName}[${objectName}],作者：${author}")
	@PostMapping("/updateByPrimaryKeySelective")
	public String updateByPrimaryKeySelective(${entityName} ${objectName}){
	JsonResult result=new JsonResult();
		try {
			int reg = ${objectName}ServiceImpl.updateByPrimaryKeySelective(${objectName});
			if (reg>0) {
				result.setCode(1);
				result.setMessage("成功");
				result.setData(${objectName});
			} else {
				logger.error("修改${objectName}失败ID："+${objectName}.toString());
				result.setCode(-1);
				result.setMessage("执行错误，请稍后重试");
			}
		} catch (Exception e) {
			logger.error("修改${objectName}执行异常："+e.getMessage());
			result.setCode(-1);
			result.setMessage("执行异常，请稍后重试");
		}
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().serializeNulls()
			.create();
			return gson.toJson(result);
	}
	
	/**
	 * @explain 获取匹配${entityName}
	 * @param   对象参数：${objectName}
	 * @return  List<${entityName}>
	 * @author  ${author}
	 * @time    ${createTime}
	 */
	@ApiOperation(value = "条件查询${entityName}", notes = "条件查询[${objectName}],作者：${author}")
	@GetMapping("/query${entityName}List")
	public String query${entityName}List(${entityName} ${objectName}){
	JsonResult result=new JsonResult();
		try {
			List<${entityName}> list = ${objectName}ServiceImpl.query${entityName}List(${objectName});
			result.setCode(1);
			result.setMessage("成功");
			result.setData(list);
		} catch (Exception e) {
			logger.error("获取${entityName}执行异常："+e.getMessage());
			result.setCode(-1);
			result.setMessage("执行异常，请稍后重试");
		}
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping().serializeNulls()
			.create();
			return gson.toJson(result);
	}
	

}